<?php

namespace merchant\controllers;

use Yii;
use yii\data\ActiveDataProvider;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yiike\store\merchant\models\OrderGoods;
use yiike\store\merchant\models\OrderStore;
use yiike\store\model\Store;

/**
 * Site controller
 */
class SiteController extends \merchant\components\Controller {

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['error'],
                        'allow'   => true,
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow'   => true,
                        'roles'   => ['@'],
                    ],
                ],
            ],
            'verbs'  => [
                'class'   => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [];
    }

    public function actionIndex()
    {
        //新订单
        $query = OrderStore::find()->select('count(id)')->where(['status' => 1]);
        if (Yii::$app->user->can('Admin') == false) {
            $query->where(['store_id' => Yii::$app->store->id]);
        }
        $new_order = $query->scalar();

        //已发货
        $query = OrderStore::find()->select('count(id)')->where(['status' => 2]);
        if (Yii::$app->user->can('Admin') == false) {
            $query->where(['store_id' => Yii::$app->store->id]);
        }
        $express_order = $query->scalar();

        //月销售额
        $query = OrderStore::find()->select('sum(t.settlement)')->where(['t.is_pay' => 1]);
        $query->alias('t');
        $query->joinWith('order o');
        $query->andWhere(['between', 'o.date', strtotime('-7 day'), time()]);
        if (Yii::$app->user->can('Admin') == false) {
            $query->where(['t.store_id' => Yii::$app->store->id]);
        }
        $total_order = $query->scalar();


        //月销售额
        $query = OrderStore::find()->select('sum(t.settlement)')->where(['t.status' => 3, 't.settlement_status' => 0]);
        $query->alias('t');
        if (Yii::$app->user->can('Admin') == false) {
            $query->where(['t.store_id' => Yii::$app->store->id]);
        }
        $settlement_order = $query->scalar();


        //近10笔订单
        $query = OrderGoods::find()
            ->limit(10)
            ->orderBy('id desc');

        if (Yii::$app->user->can('Admin') == false) {
            $query->where(['store_id' => Yii::$app->store->id]);
        }
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                 'pageSize' => 10,
             ],
        ]);
        $dataProvider->sort = false;

        if (! Yii::$app->store->id) {
            $store = Store::find()->orderBy('id desc')->one();
        }
        else {
            $store = Yii::$app->store->store;
        }

        return $this->render('index', [
            'orders' => $dataProvider,
            'new_order' => $new_order,
            'express_order' => $express_order,
            'total_order' => (double) $total_order,
            'settlement_order' => (double) $settlement_order,
            'store' => $store
            ]);
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    public function actionError()
    {
        
        $exception = Yii::$app->errorHandler->exception;
        if ($exception !== null)
        {
            $data = ['code'=> 0, 'name' => '', 'message' => $exception->getMessage(), 'exception' => $exception];
            switch ($exception->statusCode)
            {
                case 404:
                    $view = '404';
                    $data['name'] = $exception->getName();
                    break;;
                case 500:
                    $view = '500';
                    $data['name'] = $exception->getName();
                    break;
                default :
                    $view = 'error';
                    break;
            }
            
            if (Yii::$app->user->isGuest)
            {
                $this->layout = 'error';
                $view = 'error';
            }
            
            return $this->render($view, $data);
        }
    }

    public function actionMessage($message)
    {
        $this->layout = 'error';
        return parent::message($message);
    }
}
